package com.hjwoo.app.views;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hjwoo.app.R;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.been.EvaluateSuccess;
import com.hjwoo.app.been.FailureResultObject;
import com.hjwoo.app.been.QueryPriceResultObject;
import com.hjwoo.app.net.RequestListener;
import com.hjwoo.app.net.RequestMethod;
import com.hjwoo.app.net.RequestTag;
import com.hjwoo.app.utils.JsonParser;
import com.hjwoo.app.utils.LoanAccessTokenIdKeeper;
import com.hjwoo.app.utils.LoanToast;
import com.hjwoo.app.utils.Utils;

/**
 * Created by Administrator on 2016/5/6 0006.
 * 房产信息界面
 */
public class HouseInformationActivity extends BaseAppCompatActivity implements RequestListener
{

    public static final int REQUEST_CODE_01 = 1;
    public static final int REQUEST_CODE_02 = 2;
    public static final int REQUEST_CODE_03 = 3;
    public static final int REQUEST_CODE_04 = 4;
    //获取Activity实例
    public static HouseInformationActivity instance;
    public QueryPriceResultObject queryPriceResultObject = new QueryPriceResultObject();
    String city, nesidentialareaname;
    Gson gson = new Gson();
    private TextView houseinformation_city_tv, address, title_txt;
    private EditText bannumber, floornumber, housecount, accountnumber, housearea;
    private LinearLayout furnishings, orientation, housetype, housefactor;
    private TextView furnishingstv, orientationtv, housetypetv, housefactortv;
    private Button next;
    private String furnishingstvstr;
    private String a, o, c, d;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_houseinformation);
        Utils.initTitle("房产信息", this, true);
        instance = HouseInformationActivity.this;
        city = getIntent().getStringExtra("city");
        nesidentialareaname = getIntent().getStringExtra("nesidentialareaname");
        init();
    }

    private void init()
    {
        address = (TextView) findViewById(R.id.houseinformation_address_tv);//住房地址

        bannumber = (EditText) findViewById(R.id.houseinformation_bannumber_et);//楼栋信息
        floornumber = (EditText) findViewById(R.id.houseinformation_floornumber_et);//楼层信息
        housecount = (EditText) findViewById(R.id.houseinformation_housecount_et);//总层数
        accountnumber = (EditText) findViewById(R.id.houseinformation_accountnumber_et);//户号
        furnishings = (LinearLayout) findViewById(R.id.houseinformation_furnishings_rl);//居室列表
        furnishingstv = (TextView) findViewById(R.id.houseinformation_furnishings_tv);//居室列表
        orientation = (LinearLayout) findViewById(R.id.houseinformation_orientation_rl);//朝向列表
        orientationtv = (TextView) findViewById(R.id.houseinformation_orientation_tv);//朝向列表
        housearea = (EditText) findViewById(R.id.houseinformation_housearea_et);//建筑面积
        housetype = (LinearLayout) findViewById(R.id.houseinformation_housetype_rl);//物业类型
        housetypetv = (TextView) findViewById(R.id.houseinformation_housetype_tv);//物业类型
        housefactor = (LinearLayout) findViewById(R.id.houseinformation_housefactor_rl);//特殊因素
        housefactortv = (TextView) findViewById(R.id.houseinformation_housefactor_tv);//特殊因素
        next = (Button) findViewById(R.id.houseinformation_next_btn);//下一步按钮





        next.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

                next.setEnabled(false);
                doQueryPrice();

            }
        });

        houseinformation_city_tv = (TextView) findViewById(R.id.houseinformation_city_tv);//物业类型

        //设置默认值
        housetypetv.setText("普通住宅");
        orientationtv.setText("东");
        housefactortv.setText("无");

        a = furnishingstv.getText().toString().trim();
        o = orientationtv.getText().toString().trim();
        c = housetypetv.getText().toString().trim();
        d = housefactortv.getText().toString().trim();

        houseinformation_city_tv.setText(city);
        address.setText(nesidentialareaname);

        //居室列表
        furnishings.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

                Bundle b = new Bundle();
                b.putSerializable("ls", new String[]{
                    "一居室", "二居室", "三居室", "四居室", "五居室", "五居以上"
                });
                Intent i = new Intent(HouseInformationActivity.this, SelectListActivity.class);
                i.putExtras(b);
                i.putExtra("equalStr", a);
                startActivityForResult(i, REQUEST_CODE_01);
            }
        });
        //朝向列表
        orientation.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Bundle b = new Bundle();
                b.putSerializable("ls", new String[]{
                    "东", "南", "西", "北", "东西", "南北", "东南", "东北", "西南", "西北"
                });
                Intent i = new Intent(HouseInformationActivity.this, SelectListActivity.class);
                i.putExtras(b);
                i.putExtra("equalStr", o);
                startActivityForResult(i, REQUEST_CODE_02);
            }
        });
        //物业类型
        housetype.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Bundle b = new Bundle();
                b.putSerializable("ls", new String[]{
                    "普通住宅", "独栋", "联排", "双拼", "叠拼"
                });
                Intent i = new Intent(HouseInformationActivity.this, SelectListActivity.class);
                i.putExtras(b);
                i.putExtra("equalStr", c);

                startActivityForResult(i, REQUEST_CODE_03);
            }
        });
        //特殊因素
        housefactor.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Bundle b = new Bundle();
                b.putSerializable("ls", new String[]{
                    "无", "复式", "LOFT", "跃层", "一层赠送", "临湖", "楼王", "临街"
                });
                Intent i = new Intent(HouseInformationActivity.this, SelectListActivity.class);
                i.putExtras(b);
                i.putExtra("equalStr", d);

                startActivityForResult(i, REQUEST_CODE_04);

            }
        });
        title_txt = (TextView) findViewById(R.id.houseinformation_title_txt);
        title_txt.setTextColor(getResources().getColor(R.color.home_boder_color));
        findViewById(R.id.houseinformation_title_line).setBackgroundColor(getResources().getColor(R.color.home_boder_color));

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {

        if (resultCode == RESULT_OK)
        {
            //先判断是哪个页面返回过来的
            switch (requestCode)
            {
            case REQUEST_CODE_01:
                furnishingstv.setText(data.getStringExtra("itemName"));
                a = data.getStringExtra("itemName");
                break;
            case REQUEST_CODE_02:
                orientationtv.setText(data.getStringExtra("itemName"));
                o = data.getStringExtra("itemName");
                break;
            case REQUEST_CODE_03:
                housetypetv.setText(data.getStringExtra("itemName"));
                c = data.getStringExtra("itemName");
                break;
            case REQUEST_CODE_04:
                housefactortv.setText(data.getStringExtra("itemName"));
                d = data.getStringExtra("itemName");
                break;
            }
        }
    }

    /**
     * 查询
     */

    private void doQueryPrice()
    {
        //        * @param city
        //                * @param district
        //                * @param building
        //                * @param floor
        //                * @param totalFloor
        //                * @param roomNumber
        //                * @param numberOfLivingRoom
        //                * @param orientation
        //                * @param area
        //                * @param houseType
        //                * @param specialFactors
        //                * @param httpResponseHandler
        furnishingstvstr = furnishingstv.getText().toString();
        switch (furnishingstv.getText().toString())
        {
        case "一居室":
            furnishingstvstr = "1";
            break;
        case "二居室":
            furnishingstvstr = "2";
            break;
        case "三居室":
            furnishingstvstr = "3";
            break;
        case "四居室":
            furnishingstvstr = "4";
            break;
        case "五居室":
            furnishingstvstr = "5";
            break;
        case "五居以上":
            furnishingstvstr = "9";
            break;
        }
        RequestMethod.getMethod().doQueryPrice(city, address.getText().toString(), bannumber.getText().toString(), floornumber.getText().toString(),
            housecount.getText().toString(), accountnumber.getText().toString(), furnishingstvstr, orientationtv.getText().toString(),
            housearea.getText().toString(), housetypetv.getText().toString(), housefactortv.getText().toString(), LoanAccessTokenIdKeeper.readAccessToken(HouseInformationActivity.this).getAppSessionId(), HouseInformationActivity.this, HouseInformationActivity.this);

    }

    @Override
    public void requestSuccess(String tag, String message)
    {
        //不能设置为可点击,可能页面访问成功后,还没有跳转界面,报了重复单子
        //    next.setEnabled(true);
        switch (tag)
        {
        case RequestTag.APPQUERYPRICE:
            queryPriceResultObject = gson.fromJson(message, QueryPriceResultObject.class);
            RequestMethod.getMethod().appcreate(queryPriceResultObject.getUnitPrice(), housearea.getText().toString(),
                housefactortv.getText().toString(), orientationtv.getText().toString(),
                accountnumber.getText().toString(), city,
                housetypetv.getText().toString(), queryPriceResultObject.getLoanAmount(),
                bannumber.getText().toString(), furnishingstvstr,
                address.getText().toString(), housecount.getText().toString(),
                floornumber.getText().toString(),
                HouseInformationActivity.this, HouseInformationActivity.this);
            break;
        case RequestTag.APPCREATE:
            EvaluateSuccess evalua = JsonParser.getJsonParser().parserJsonToObj(message, EvaluateSuccess.class);
            Intent intent = new Intent(HouseInformationActivity.this, EvaluateSuccessActivity.class);
            intent.putExtra("unitPrice", queryPriceResultObject.getUnitPrice());
            intent.putExtra("loanAmount", queryPriceResultObject.getLoanAmount());
            intent.putExtra("minLoanAmount", queryPriceResultObject.getMinLoanAmount());
            intent.putExtra("serialNumber", evalua.serialNumber);
            startActivity(intent);
            break;
        }
    }

    @Override
    public void requestError(String message)
    {
        next.setEnabled(true);
        FailureResultObject failureResultObject = new FailureResultObject();
        failureResultObject = gson.fromJson(message, FailureResultObject.class);
        LoanToast.showToastShort(failureResultObject.getErrorMessage(), HouseInformationActivity.this);
    }
}
